package com.zcoj.controller.Admin.group;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.*;
import com.zcoj.pojo.Vo.GroupAnnouncementVO;
import com.zcoj.pojo.Vo.GroupRegisterVO;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.*;
import com.zcoj.util.RedisCaches;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/19 10:16
 */
@RequestMapping("/adminGroup")
@RestController
@PreAuthorize("hasAnyAuthority('super_admin','admin','group_admin')")
public class adminGroupController {


    @Autowired
    private GroupService groupService;

    @Autowired
    private GroupProblemService groupProblemService;

    @Autowired
    private GroupAnnouncementService groupAnnouncementService;


    @Autowired
    private AnnouncementService announcementService;


    @Autowired
    private GroupRegisterService groupRegisterService;

    @Autowired
    private UserService userService;
    @Autowired
    private RedisCaches redisCaches;


    @PostMapping("/createGroup")
    public ApiResponse createGroup(@RequestBody Group group){
        return ApiResponse.success(groupService.adminCreate(group));
    }

    @PostMapping("/groupList")
    public ApiResponse groupList(@RequestBody PageWrap<Group> pageWrap){
        return ApiResponse.success(groupService.findPage(pageWrap));
    }


    @GetMapping("/delGroup/{id}")
    public ApiResponse delGroup(@PathVariable Integer id){
        Integer integer = groupService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功",null);
        }else {
            return ApiResponse.failed("删除失败！！！");
        }
    }


    @PostMapping("/updateGroup")
    public ApiResponse updateGroup(@RequestBody Group group){
        Integer updateById = groupService.updateById(group);
        if(updateById!=0){
            return ApiResponse.success("团队信息更新成功",null);
        }else {
            return ApiResponse.failed("团队信息更新失败！！！");
        }
    }


    @GetMapping("/selectedGroupProblemList/{gid}")
    public ApiResponse selectedGroupProblemList(@PathVariable Integer gid){
        GroupProblem groupProblem = new GroupProblem();
        groupProblem.setGid(gid);
        List<GroupProblem> groupProblemServiceList = groupProblemService.findList(groupProblem);
        List<Integer> problemIdList=new ArrayList<>();
        for (GroupProblem problem : groupProblemServiceList) {
            problemIdList.add(problem.getPid());
        }
        return ApiResponse.success("",problemIdList);
    }

    @GetMapping("/getGroupById/{id}")
    public ApiResponse getGroupById(@PathVariable Integer id){
        return ApiResponse.success( groupService.findById(id));
    }


    @GetMapping("/appendGroupProblemData/{gid}")
    public ApiResponse appendGroupProblemData(@PathVariable("gid") Integer gid,@RequestParam("pids") List<String> pids){
        Integer sum=0;
        for (String pid : pids) {
            GroupProblem groupProblem = new GroupProblem();
            groupProblem.setPid(Integer.parseInt(pid));
            groupProblem.setGid(gid);
            groupProblemService.create(groupProblem);
            sum++;
        }
        return ApiResponse.success(sum+"条题目数据添加完成",null);
    }

@GetMapping("/editGroupProblemData/{gid}")
public ApiResponse editGroupProblemData(@PathVariable("gid") Integer gid,@RequestParam("pids") List<String> pids){
    Integer sum=0;
    GroupProblem gp = new GroupProblem();
    gp.setGid(gid);
    List<GroupProblem> groupProblemServiceList = groupProblemService.findList(gp);

    for (GroupProblem groupProblem : groupProblemServiceList) {
        groupProblemService.deleteById(groupProblem.getId());
    }
    System.out.println(pids);
    for (String pid : pids) {
        GroupProblem groupProblem = new GroupProblem();
        groupProblem.setPid(Integer.parseInt(pid));
        groupProblem.setGid(gid);
        groupProblemService.create(groupProblem);
        sum++;
    }
    return ApiResponse.success(sum+"条题目数据更新完成",null);
}




    @GetMapping("/getGroupAnnouncement/{gid}")
    public ApiResponse getGroupAnnouncement(@PathVariable Integer gid){

        GroupAnnouncement groupAnnouncement = new GroupAnnouncement();
        groupAnnouncement.setGid(gid);
        GroupAnnouncement groupAnnouncementServiceOne = groupAnnouncementService.findOne(groupAnnouncement);
        Announcement announcement=null;
        if( Objects.nonNull(groupAnnouncementServiceOne)){
            announcement = announcementService.findById(groupAnnouncementServiceOne.getAid());
        }

        return ApiResponse.success("",announcement);
    }

    @PostMapping("/createGroupAnnouncement")
    public ApiResponse createGroupAnnouncement(@RequestBody GroupAnnouncementVO groupAnnouncementVO){
        LoginUser loginUser = redisCaches.getLoginUser();
        Announcement announcement = new Announcement();
        announcement.setContent(groupAnnouncementVO.getContent());
        announcement.setTitle(groupAnnouncementVO.getTitle());
        announcement.setStatus(groupAnnouncementVO.getStatus());
        announcement.setUid(loginUser.getId());
        announcement.setAuthor(loginUser.getUsername());
        announcement.setGid(groupAnnouncementVO.getGid());
        if(Objects.nonNull(groupAnnouncementVO.getId())){
            announcement.setId(groupAnnouncementVO.getId());
            announcementService.updateById(announcement);
            return ApiResponse.success("公告更新成功");
        }else {
            Integer aid = announcementService.create(announcement);
            GroupAnnouncement groupAnnouncement = new GroupAnnouncement();
            groupAnnouncement.setAid(aid);
            groupAnnouncement.setGid(groupAnnouncementVO.getGid());
            Integer gAId = groupAnnouncementService.create(groupAnnouncement);
            if(aid!=0&&gAId!=0){
                return ApiResponse.success("公告添加成功");
            }else {
                return ApiResponse.failed("公告添加失败！！！");
            }
        }

    }

    @PostMapping("/updateGroupAnnouncement")
    public ApiResponse updateGroupAnnouncement(@RequestBody GroupAnnouncementVO groupAnnouncementVO){
        LoginUser loginUser = redisCaches.getLoginUser();
        Announcement announcement = new Announcement();
        announcement.setContent(groupAnnouncementVO.getContent());
        announcement.setTitle(groupAnnouncementVO.getTitle());
        announcement.setStatus(groupAnnouncementVO.getStatus());
        announcement.setUid(loginUser.getId());
        announcement.setAuthor(loginUser.getUsername());

        Integer aid = announcementService.create(announcement);
        GroupAnnouncement groupAnnouncement = new GroupAnnouncement();
        groupAnnouncement.setAid(aid);
        groupAnnouncement.setGid(groupAnnouncementVO.getGid());
        Integer gAId = groupAnnouncementService.create(groupAnnouncement);
        if(aid!=0&&gAId!=0){
            return ApiResponse.success("公告添加成功");
        }else {
            return ApiResponse.failed("公告添加失败！！！");
        }
    }


    @GetMapping("/getGroupRegisterStatus/{status}")
    public ApiResponse getGroupRegisterStatus(@PathVariable Integer status){
        GroupRegister groupRegister = new GroupRegister();
        groupRegister.setStatus(status);
        List<GroupRegister> list = groupRegisterService.findList(groupRegister);
        List<GroupRegisterVO> groupRegisterVOList=new ArrayList<>();
        for (GroupRegister register : list) {
            GroupRegisterVO groupRegisterVO = new GroupRegisterVO();
            groupRegisterVO.setGroupRegister(register);
            User userServiceById = userService.findById(register.getUid());
            groupRegisterVO.setUname(userServiceById.getUsername());
            Group groupServiceById = groupService.findById(register.getGid());
            groupRegisterVO.setGname(groupServiceById.getName());
            groupRegisterVOList.add(groupRegisterVO);
        }
        return ApiResponse.success(groupRegisterVOList);
    }


    @GetMapping("/passGroupRegister/{id}")
    public ApiResponse passGroupRegister(@PathVariable Integer id){
        System.out.println("-----"+id);
        GroupRegister byId = groupRegisterService.findById(id);

        if(Objects.nonNull(byId)){

            byId.setStatus(2);
            groupRegisterService.updateById(byId);

            Group groupServiceById = groupService.findById(byId.getGid());
            if(Objects.nonNull(groupServiceById)){
                groupServiceById.setStatus(1);
                groupService.updateById(groupServiceById);
                return ApiResponse.success("修改成功",null);
            }else {
                return ApiResponse.failed("团队不存在");
            }

        }else {
            return ApiResponse.failed("参数非法");
        }

    }


    @GetMapping("/noPassGroupRegister/{id}")
    public ApiResponse noPassGroupRegister(@PathVariable Integer id){
        GroupRegister byId = groupRegisterService.findById(id);
        if(Objects.nonNull(byId)){
            byId.setStatus(0);
            Group groupServiceById = groupService.findById(byId.getGid());
            groupServiceById.setStatus(0);
            groupRegisterService.updateById(byId);
            groupService.updateById(groupServiceById);
            return ApiResponse.success("修改成功",null);
        }else {
            return ApiResponse.failed("参数非法");
        }


    }




}
